# 本程序主要用来描述matplotlib的具体使用
# 导入相应的库
import numpy as np
from matplotlib import pyplot as plt

fig = plt.figure(figsize=(8, 6), dpi=100, facecolor='w')  # 创建一个尺寸为(8,6)，分辨率为100dpi，背景色为青色的画布
plt.show()
# 正弦
x = np.linspace(0, 2 * np.pi, 200)  # 点密集后，折线图会变得光滑
y = np.sin(x)
fig1 = plt.figure()
plt.plot(x, y, linestyle='-', color='c')
plt.title('Line Plot', fontsize=18)
plt.xlabel('x', loc='right')
plt.ylabel('y', loc='top', rotation=0)
plt.grid(axis='y', linestyle='--', color='k', alpha=0.5)  # 只显示y轴网格线
plt.show()
# 余弦
x = np.linspace(0, 2 * np.pi, 200)  # 点密集后，折线图会变得光滑
y = np.cos(x)
fig2 = plt.figure()
plt.plot(x, y, linestyle='-', color='c')
plt.title('Line Plot', fontsize=18)
plt.xlabel('x', loc='right')
plt.ylabel('y', loc='top', rotation=0)
plt.grid(axis='y', linestyle='--', color='k', alpha=0.5)  # 只显示y轴网格线
plt.show()

# fig1 = plt.figure()  # 全部用默认值
# x = [12, 24, 35, 42, 54]
# y = [22, 1, 32, 51, 42]
#
# x1 = [14, 25, 36, 47, 53]
# y1 = [25, 15, 3, 5, 4]
#
# x2 = [14, 24, 44, 42, 54]
# y2 = [26, 1, 2, 51, 42]
#
# x3 = [19, 26, 35, 42, 64]
# y3 = [2, 1, 32, 54, 4]
# plt.plot(x, y, color='b', linestyle='--', marker='*', markerfacecolor='c', markersize=15)
# plt.plot(x1, y1, color='b', linestyle='--', marker='<', markerfacecolor='c', markersize=15)
# plt.plot(x2, y2, color='b', linestyle='--', marker='o', markerfacecolor='c', markersize=15)
# plt.plot(x3, y3, color='b', linestyle='--', marker='<', markerfacecolor='c', markersize=15)
# plt.show()  # 每次调用plt.show()方法后，会将所有绘图元素释放出来，所以对元素的修改应该在该方法之前
#
# x = np.linspace(0, 2 * np.pi, 200)  # 点密集后，折线图会变得光滑
# y = np.sin(x)
# fig2 = plt.figure()
# plt.plot(x, y, linestyle='-', color='c')
# plt.title('Line Plot', fontsize=18)
# plt.xlabel('x', loc='right')
# plt.ylabel('y', loc='top', rotation=0)
# plt.grid(axis='y', linestyle='--', color='k', alpha=0.5)  # 只显示y轴网格线
# plt.show()
#
# x = np.linspace(0, 2 * np.pi, 200)
# y1 = np.sin(x)
# y2 = np.cos(x)
# fig3 = plt.figure()
# plt.plot(x, y1, linestyle='-', color='c', label='y=sin(x)')  # 该线标记为y=sin(x)
# plt.plot(x, y2, linestyle='--', color='m', label='y=cos(x)')  # 该线标记为y=cos(x)
# plt.title('y=sin(x) and y=cos(x)')
# plt.xlabel('x', loc='right')
# plt.ylabel('y', loc='top', rotation=0)
# x_ticks = np.linspace(0, 2 * np.pi, 5)
# x_ticks_labels = ['0', 'π/2', 'π', '3π/2', '2π']
# y_ticks = np.linspace(-1, 1, 5)
# plt.xticks(x_ticks, x_ticks_labels, c='g')
# plt.yticks(y_ticks)
# plt.legend(loc='best')
# plt.grid(linestyle='--', axis='y', alpha=0.5)
# plt.show()
#
# x1 = np.linspace(0, 10, 200)
# y1 = np.exp(x1)
# x2 = np.linspace(0.001, 10, 200)
# y2 = np.log(x2)
# fig4 = plt.figure()
# ax1 = fig.add_subplot()
# ax1.plot(x1, y1, linestyle='-.', color='b', alpha=0.8, label='y=e^x')
# ax1.tick_params(axis='y', labelcolor='b')  # 将对应的y轴刻度标签改为对应颜色
# ax2 = ax1.twinx()
# ax2.plot(x2, y2, linestyle=':', color='g', alpha=0.8, label='y=ln(x)')
# ax2.tick_params(axis='y', labelcolor='g')  # 将对应的y轴刻度标签改为对应颜色
# plt.title('y=e^x and y=ln(x)')
# plt.show()
#
# fig5 = plt.figure(facecolor='lightblue')
#
# x1 = np.linspace(0, 2 * np.pi, 200)
# y1 = np.sin(x1)
# plt.subplot(2, 2, 1)  # 一共2×2个子图，此为第一个
# plt.plot(x1, y1, linestyle='-', color='b', label='y=sin(x)')
# plt.title('y=sin(x)')
# plt.xlabel('x', loc='right')
# plt.ylabel('y', loc='top')
# plt.grid(alpha=0.8)
# x_ticks = np.linspace(0, 2 * np.pi, 5)
# x_labels = ['0', 'π/2', 'π', '3π/2', '2π']
# plt.xticks(x_ticks, x_labels)
# plt.yticks(np.linspace(-1, 1, 5))
#
# x2 = np.linspace(0, 2 * np.pi, 200)
# y2 = np.cos(x2)
# plt.subplot(2, 2, 2)  # 一共2×2个子图，此为第二个
# plt.plot(x2, y2, linestyle='--', color='g', label='y=cos(x)')
# plt.title('y=cos(x)')
# plt.xlabel('x', loc='right')
# plt.ylabel('y', loc='top')
# plt.grid(axis='y', alpha=0.5)
# plt.xticks(x_ticks, x_labels)
# plt.yticks(np.linspace(-1, 1, 5))
#
# x3 = np.linspace(0, 10, 200)
# y3 = np.exp(x3)
# plt.subplot(2, 2, 3)  # 一共2×2个子图，此为第三个
# plt.plot(x3, y3, linestyle='-.', color='orange', alpha=0.7, label='y=e^x')
# plt.title('y=e^x')
# plt.xlabel('x', loc='right')
# plt.ylabel('y', loc='top')
#
# x4 = np.linspace(0.01, 10, 200)
# y4 = np.log10(x4)
# plt.subplot(2, 2, 4)  # 一共2×2个子图，此为第四个
# plt.plot(x4, y4, linestyle=':', color='r', alpha=0.6, label='y=lg(x)')
# plt.title('y=lg(x)')
# plt.xlabel('x', loc='right')
# plt.ylabel('y', loc='top')
#
# plt.tight_layout()  # 自动调整布局（紧凑布局）
# plt.show()
#
# fig6 = plt.figure()
# x1 = np.linspace(0, 2 * np.pi, 200)
# y1 = np.sin(x1)
# plt.plot(x1, y1)
# plt.title('y=sin(x)')
# x2 = np.linspace(0, 2 * np.pi, 200)
# y2 = np.cos(x2)
# ax2 = fig.add_axes([0.58, 0.55, 0.25, 0.25])
# ax2.set_title('y=cos(x)')  # 用轴域绘图时设置标签等方法一般在前面加set
# ax2.plot(x2, y2)  # 如ax.set_xticks()、ax.set_title()等
# plt.show()
#
# x = np.arange(1, 6)
# y = np.random.randint(60, 100, 5)
# fig7 = plt.figure()
# plt.bar(x, y, color=['c', 'm', 'r', 'g', 'b'], width=0.5, alpha=0.5)
# plt.title('Revenue-Month Plot')
# plt.xlabel('month', loc='right')
# plt.ylabel('revenue', loc='top')
# plt.show()
#
# x = np.arange(1, 6)
# x_labels = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
# data_Tom = np.random.randint(60, 100, size=5)
# data_Jack = np.random.randint(60, 100, size=5)
# data_Wilson = np.random.randint(60, 100, size=5)
# fig8 = plt.figure()
# width = 0.3
# plt.bar(x - width, data_Tom, color='#D98481', width=width, label='Tom')
# plt.bar(x, data_Jack, color='#91B5A9', width=width, label='Jack')
# plt.bar(x + width, data_Wilson, color='#EDCA7F', width=width, label='Wilson')
# plt.title('Revenue-Month Plot')
# plt.xlabel('Month', loc='right')
# plt.ylabel('Revenue', loc='top')
# plt.xticks(x, x_labels)
# plt.legend(loc='best')
# plt.show()
#
# x = np.arange(4)
# x_labels = ['Tom', 'Jack', 'Mike', 'John']
# data_Jan = np.random.randint(60, 100, size=4)
# data_Feb = np.random.randint(60, 100, size=4)
# data_Mar = np.random.randint(60, 100, size=4)
# fig9 = plt.figure()
# plt.bar(x, data_Jan, color='#D98481', label='Jan')
# plt.bar(x, data_Feb, color='#91B5A9', bottom=data_Jan, label='Feb')
# plt.bar(x, data_Mar, color='#EDCA7F', bottom=data_Jan + data_Feb, label='Mar')
# plt.title('Revenue-Staff Plot')
# plt.xticks(x, x_labels)
# plt.xlabel('Staff', loc='right')
# plt.ylabel('Revenue', loc='top')
# plt.legend(loc='best')
# plt.show()
#
# x = np.arange(5)
# y = np.random.randint(50, 100, size=5)
# fig10 = plt.figure()
# plt.barh(x, y, color=['b', 'g', 'r', 'c', 'm'], alpha=0.5)
# plt.title('Horizontal Bar Plot')
# plt.xlabel('y', loc='right')
# plt.ylabel('x', loc='top')
# plt.show()
#
# x = np.arange(10)
# y = np.random.randint(50, 100, size=10)
# fig11 = plt.figure()
# plt.stackplot(x, y, color='c', alpha=0.5)
# plt.title('Area Plot')
# plt.xlabel('x', loc='right')
# plt.ylabel('y', loc='top')
# plt.show()
#
# x = np.linspace(0, 100, 2000)
# y = np.sin(x) * np.exp(-0.1 * x)
# y1 = np.exp(-0.1 * x)
# y2 = -np.exp(-0.1 * x)
# fig12 = plt.figure()
# plt.plot(x, y, color='c')
# plt.title('y=sin(x)·e^(-0.1x)')
# plt.xlabel('x', loc='right')
# plt.ylabel('y', loc='top')
# plt.fill_between(x, y1, y2, color='y', alpha=0.4)
# plt.show()
#
# x = np.arange(10)
# y = np.random.randn(10)
# fig13 = plt.figure()
# plt.scatter(x, y, color='lightblue', marker='*', linewidth=0.5, edgecolor='b', s=100)
# plt.title('Scatter Plot')
# plt.xlabel('x', loc='right')
# plt.ylabel('y', loc='top')
# plt.show()
#
# x = np.random.rand(100)
# y = np.random.randn(100)
# s = np.random.rand(100)  # 随机指定点的大小
# c = np.random.rand(100)  # 随机指定点颜色的映射值
# fig14 = plt.figure()
# plt.scatter(x, y, s=s * 500, c=c, cmap='rainbow', alpha=0.8)
# plt.title('Bubble Plot')
# plt.xlabel('x', loc='right')
# plt.ylabel('y', loc='top')
# plt.show()
#
# data = np.random.randn(1000)
# fig15 = plt.figure()
# plt.hist(data, bins=10, color='lightblue')
# plt.title('Histogram Plot')
# plt.show()
#
# data = np.random.randint(50, 100, size=5)
# labels = ['Alice', 'Bob', 'Tom', 'Jack', 'Mike']
# explode = [0, 0, 0.1, 0, 0]
# fig16 = plt.figure()
# plt.pie(data, labels=labels, colors=['#B1D5E4', '#DB7777', '#5EB376', '#929FC8', '#DC7F68'],
#         autopct='%1.1f%%', shadow=True, explode=explode, pctdistance=0.5, labeldistance=1.1,
#         wedgeprops={'edgecolor': 'w', 'width': 1})
# # autopct='%1.1f%%'表示百分比保留一位小数，若autopct='%1.2f%%'则百分比保留两位小数
# plt.title('Pie Plot')
# plt.show()
#
# data = np.random.randint(50, 100, size=5)
# labels = ['Alice', 'Tom', 'Jack', 'Mike', 'Tony']
# fig17 = plt.figure()
# plt.pie(data, labels=labels, colors=['#B1D5E4', '#DB7777', '#5EB376', '#929FC8', '#DC7F68'],
#         autopct='%1.1f%%', wedgeprops={'width': 0.5, 'edgecolor': 'w'}, pctdistance=0.7)
# plt.title('Donut Chart')
# plt.show()
#
# data1 = np.random.randint(50, 100, size=5)
# data2 = np.random.randint(50, 100, size=5)
# fig18 = plt.figure()
# plt.pie(data1, colors=['#B1D5E4', '#DB7777', '#5EB376', '#929FC8', '#DC7F68'],
#         wedgeprops={'width': 0.5, 'edgecolor': 'w'})
# plt.pie(data2, colors=['#7DBFB4', '#E1BE54', '#B2516B', '#2E8391', '#CDA4BF'],
#         radius=0.4)
# plt.show()
#
# data = np.random.randn(100)
# fig19 = plt.figure()
# plt.boxplot(data)
# plt.title('Box Plot')
# plt.show()
#
# x = np.linspace(-5, 5, 200)
# y = np.linspace(-5, 5, 200)
# X, Y = np.meshgrid(x, y)
# Z = X ** 2 + Y ** 2
# fig20 = plt.figure()
# contour = plt.contour(X, Y, Z, levels=10, cmap='rainbow')
# plt.clabel(contour, inline='True', fontsize=8)
# plt.xlabel('x')
# plt.ylabel('y')
# plt.title('Contour Plot')
# plt.show()
#
# x = np.linspace(-5, 5, 200)
# y = np.linspace(-5, 5, 200)
# X, Y = np.meshgrid(x, y)
# Z = np.sin(np.sqrt(X ** 2 + Y ** 2))
# fig21 = plt.figure()
# plt.contourf(X, Y, Z, cmap='viridis')
# plt.colorbar()
# plt.xlabel('x')
# plt.ylabel('y')
# plt.title('Contourf Plot')
# plt.show()
#
# fig22 = plt.figure()
# ax = fig.add_subplot(projection='polar')
# x = np.linspace(0, 2 * np.pi, 5)
# y = np.random.randint(60, 100, size=5)
# ax.plot(x, y)
# plt.title('Polar Plot')
# plt.show()
#
# fig23 = plt.figure()
# ax = fig.add_subplot(projection='polar')
# x_0 = np.linspace(0, 2 * np.pi, 7)
# x = np.hstack((x_0, x_0[0]))  # 这一步是为了将起始点和终止点连接起来
# y_0 = np.full(shape=7, fill_value=100)
# y = np.hstack((y_0, y_0[0]))  # 这一步是为了将起始点和终止点连接起来
# ax.plot(x, y, color='#7DBFB4')
# ax.fill_between(x, y, 0, color='lightblue', alpha=0.8)
# x_ticks = np.linspace(0, 5 * np.pi / 3, 6)
# x_labels = ['power', 'experience', 'speed', 'defence', 'service', 'skills']
# ax.set_xticks(x_ticks, x_labels, color='#B2516B')
# ax.set_yticks([])  # 关闭y轴刻度
# plt.title('Ma Long')
# plt.show()
#
# x = np.arange(1, 6)
# x_label = ['Alex', 'Bruce', 'Derrick', 'Eric', 'Frank']
# color = ['b', 'g', 'c', 'm', 'r']
# y = np.random.randint(60, 100, size=5)
# fig24 = plt.figure()
# plt.bar(x, y, color=color, alpha=0.5)
# plt.xticks(x, x_label)
# plt.title('Bar Plot')
# for (a, b) in zip(x, y):
#     plt.text(x=a, y=b, s=b, ha='center', va='bottom')
# plt.show()
#
# x = np.linspace(0, 2 * np.pi, 200)
# y = np.sin(x)
# fig25 = plt.figure()
# plt.plot(x, y, c='lightblue')
# plt.xlim([-1, 8])
# plt.ylim([-2, 2])
# # plt.xlim([low,high])、plt.ylim([low,high])表示显示的x、y轴的范围
# # 以上函数等效于plt.axis([-1,8,-2,2])
# plt.xlabel('x', loc='right')
# plt.ylabel('y', loc='top')
# plt.title('y=sin(x)')
# plt.annotate(text='maximum', xy=(np.pi / 2, 1), c='#DB7777', xytext=(0.5 + np.pi / 2, 1.5),
#              arrowprops={'width': 0.5, 'headwidth': 3, 'color': '#2E8391'})
# plt.annotate(text='minimum', xy=(3 * np.pi / 2, -1), c='#DB7777', xytext=(0.5 + 3 * np.pi / 2, -1.5),
#              arrowprops={'width': 0.5, 'headwidth': 3, 'color': '#7DBFB4'})
# plt.show()
